package a03_哈希;

import java.util.HashSet;
import java.util.Set;

/**
 * <p>
 * a03_快乐数
 * </p>
 *
 * @author flyduck
 * @since 2024-06-24
 */
public class a04_快乐数复习3 {

    public static void main(String[] args) {
        int n = 19;

//        System.out.println(getNext(129));
/*
        System.out.println(getSquareSum(n));
*/
        System.out.println(isHappy(n));
    }


    public static boolean isHappy(int n) {
        Set<Integer> set = new HashSet<>();
        while (n > 1){
            n = getNext(n);
            if(set.contains(n)){
                return false;
            }
            set.add(n);
        }
        return true;
    }

    //129 / 10 = 12(9)
    //12
    //1
    public static int getNext(int n){
        int sum = 0;
        while (n > 0){
            sum = sum + ((n % 10) * (n % 10));
            n = n / 10;
        }
        return sum;
    }
}
